import { faker } from "@faker-js/faker";

/**
 * User permission mock
 */
const DASHBOARD_PERMISSION = {
  name: "Dashboard",
  route: "dashboard",
  children: [
    {
      name: "Showcase",
      route: "showcase",
      component: "/dashboard/showcase/index.tsx",
    },
  ],
};
const WORKBEN_PERMISSION = {
  name: "Workbench",
  route: "workbench",
  children: [],
};

export const PERMISSION_LIST = [DASHBOARD_PERMISSION, WORKBEN_PERMISSION];

/**
 * User role mock
 */
const ADMIN_ROLE = {
  id: "4281707933534332",
  name: "Admin",
  label: "admin",
  order: 1,
  desc: "超级管理员",
  permission: PERMISSION_LIST,
};
const DEFAULT_ROLE = {
  id: "9931665660771476",
  name: "xiaoer",
  label: "小二",
  order: 2,
  desc: "运营小二",
  permission: [WORKBEN_PERMISSION],
};
export const ROLE_LIST = [ADMIN_ROLE, DEFAULT_ROLE];

/**
 * User data mock
 */
export const ADMIN_USER = {
  id: faker.string.uuid(),
  username: "admin123",
  email: faker.internet.email(),
  phone: "19969265879",
  avatar: "/img/admin123.png",
  password: "xiaoyu@LY123",
  role: ADMIN_ROLE,
  permissions: ADMIN_ROLE.permission,
  token: faker.string.uuid(),
};
export const DEFAULT_USER = {
  id: faker.string.uuid(),
  username: "xiaoer123",
  password: "xiaoyu@LY123",
  phone: faker.phone.number(),
  email: faker.internet.email(),
  avatar: "/img/xiaoer123.png",
  role: DEFAULT_ROLE,
  permissions: DEFAULT_ROLE.permission,
  token: faker.string.uuid(),
};
export const USER_LIST = [ADMIN_USER, DEFAULT_USER];

// 注册新增
export function doRegister(user) {
  Object.assign(user, {
    id: faker.string.uuid(),
    role: DEFAULT_ROLE,
    permissions: DEFAULT_ROLE.permission,
    token: faker.string.uuid(),
    avatar: ""
  }); 
  USER_LIST.push(user);
}

/**
 * ManageTableList
 *  */
// 存储商品项的数据存储表
let PRODUCT_TABLE = [];
// 设置登录角色
let USER = {};
const PRODUCT_NAME = ["爱奇艺会员", "QQ音乐会员", "2024新款智能手表"];
const CREATOR_NAME = ["小雨", "xiaoer123", "react"];

// 设置一个中间存储层
const PRODUCT_TABLE_MID = [];

// 登录成功时，凭借身份新增一批数据，后续新增商品在继续添加
export function addProduct(manager = "admin", product) {
  const valueEnum = {
    0: "close",
    1: "reviewing",
    2: "online",
  };
  const name = PRODUCT_NAME[Math.floor(Math.random() * PRODUCT_NAME.length)];
  const product_basicInfo = {
    key: faker.string.nanoid(6),
    productID: faker.string.nanoid(10),
    name,
    containers: Math.floor(Math.random() * 200) + 10,
    shelf_on: Date.now() - Math.floor(Math.random() * 100000 ** 2),
    shelf_off: Date.now() - Math.floor(Math.random() * 100000 * 10000),
    creator:
      manager === "admin"
        ? CREATOR_NAME[Math.floor(Math.random() * CREATOR_NAME.length)]
        : manager,
    status:
      valueEnum[
        (Math.floor(Math.random() * 10) % Object.keys(valueEnum).length) + ""
      ],
    pic: "",
    desc: faker.lorem.text(),
    type: "无", //商品类型
    category: "虚拟货物", //商品分类
    vendor: "小雨", //供应商
    phone: faker.phone.number(),
    security: faker.lorem.text(), //服务保障
    blacklist: faker.location.city(),
    whitelist: faker.location.city(),
    bound: faker.number.int({ min: 10, max: 10000 }), // 42,//兑换上限
    operates: [],
  };
  if (name === "爱奇艺会员") {
    product_basicInfo.pic = "/img/aiqiyi.jpg";
  } else if (name === "QQ音乐会员") {
    product_basicInfo.pic = "/img/qq.jpg";
  } else if (name === "2024新款智能手表") {
    product_basicInfo.pic = "/img/watch.jpg";
  }
  let status = product_basicInfo.status;
  if (status === "close") {
  } else if (status === "reviewing") {
    product_basicInfo.shelf_on = "";
    product_basicInfo.shelf_off = "";
  } else if (status === "online") {
    product_basicInfo.shelf_off = "";
  }

  if (manager === "admin") {
    product_basicInfo.operates.push({
      key: faker.string.nanoid(6),
      operatID: faker.string.nanoid(8),
      operatTime: new Date().toLocaleDateString(),
      operatPerson: "小雨",
      status: "发起审批",
      comment: "准备上线一个商品了~~~\n准备上线一个盈利的商品了~~~",
    });
    PRODUCT_TABLE.push(product_basicInfo);
  } else if (product !== undefined) {
    PRODUCT_TABLE.push(product);
  } else {
    Object.assign(valueEnum, { 3: "error", 4: "success" });
    product_basicInfo.status =
      valueEnum[
        (Math.floor(Math.random() * 10) % Object.keys(valueEnum).length) + ""
      ];
    let status = product_basicInfo.status;
    if (status === "close") {
      product_basicInfo.shelf_on =
        Date.now() - Math.floor(Math.random() * 100000 ** 2);
      product_basicInfo.shelf_off =
        Date.now() - Math.floor(Math.random() * 100000 * 10000);
    } else if (
      status === "reviewing" ||
      status === "error" ||
      status === "success"
    ) {
      product_basicInfo.shelf_on = "";
      product_basicInfo.shelf_off = "";
    } else if (status === "online") {
      product_basicInfo.shelf_off = "";
      product_basicInfo.shelf_on =
        Date.now() - Math.floor(Math.random() * 100000 ** 2);
    }
    PRODUCT_TABLE.push(product_basicInfo);
  }
}
export function resetData() {
  PRODUCT_TABLE = [];
  USER = {};
}
export function changeUser(user) {
  USER = user;
}
export function Initialization() {
  for (let i = 0; i < 10; i++) {
    if (USER.role.label === "admin") {
      for (let u = 0; u < 5; u++) {
        addProduct();
      }
    } else if(USER.avatar===""){
    }else{
      addProduct(USER.username);

    }
  }
  // 输入中间层商品
  if(USER.avatar!==""){
    PRODUCT_TABLE.unshift(...PRODUCT_TABLE_MID);
  }
}
// 获取数据
export function getManageList({ params, sorter, filter }) {
  // 简单的一个过滤操作

  if (params.productID !== undefined && params.name !== undefined) {
    return PRODUCT_TABLE.filter(
      (item) => item.productID === params.productID && item.name === params.name
    );
  } else if (params.productID !== undefined) {
    return PRODUCT_TABLE.filter((item) => item.productID === params.productID);
  } else if (params.name !== undefined) {
    return PRODUCT_TABLE.filter((item) => item.name === params.name);
  }
  return PRODUCT_TABLE;
}
export function getProductDetailByID(productID) {
  return PRODUCT_TABLE.find((item) => item.productID === productID);
}
// 根据ID下线商品
export function shelfOff(productID) {
  const product = PRODUCT_TABLE.find((item) => item.productID === productID);
  product.status = "close";
}
// 根据ID上线商品
export function shelfOn(productID) {
  const product = PRODUCT_TABLE.find((item) => item.productID === productID);
  product.status = "online";
}
// 对商品进行操作
export function operateForProduct({
  productID,
  operatPerson,
  status, // 下架商品
  comment,
}) {
  const product = PRODUCT_TABLE.find((item) => item.productID === productID);
  product.operates.unshift({
    key: faker.string.nanoid(6),
    operatID: faker.string.nanoid(8),
    operatTime: new Date().toLocaleDateString(),
    operatPerson,
    status,
    comment,
  });
  if (status === "下架商品") {
    product.shelf_off = new Date();
  } else if (status === "审批通过") {
    product.status = "success";
  } else if (status === "审批失败") {
    product.status = "error";
  } else if (status === "发起审批") {
    product.status = "reviewing";
    PRODUCT_TABLE_MID.unshift(product);
  } else if (status === "上线") {
    product.status = "online";
  }
}

// 生成随机的商品类别
const generateRandomGoodsCategory = () => {
  const categories = [
    "生鲜",
    "淋浴用品",
    "脸部护理",
    "烫发染发",
    "洗手护手",
    "童鞋童装",
    "足部护理",
    "抽纸卷纸",
    "湿巾清洁",
    "精致洗衣",
    "衣物护理",
    "厨卫清洁",
    "母婴用品",
    "家庭护理",
    "瘦身健体",
    "洗护用品",
    "视频会员",
    "音乐会员",
    "公交地铁",
    "蚂蚁森林道具",
    "精品鞋袜",
    "蚂蚁森林道具",
    "公交地铁",
    "音乐会员",
    "水果蔬菜",
    "猫粮狗粮",
    "速冻食品",
    "海鲜水产",
  ];
  return categories[Math.floor(Math.random() * categories.length)];
};

// 生成2024年6月内随机的一天
const generateRandomSaleTime = () => {
  const year = 2024;
  const month = 5;
  const day = Math.floor(Math.random() * 30) + 1;
  return new Date(year, month, day).toISOString();
};

export function goodSaleList(num) {
  const list = [];
  for (let i = 1; i <= num; i++) {
    list.push({
      goods_id: Math.floor(Math.random() * 1000) + 1,
      goods_name: faker.commerce.productName(),
      goods_category: generateRandomGoodsCategory(),
      pay_type: Math.floor(Math.random() * 3) + 1,
      sale_time: generateRandomSaleTime(),
      count: Math.floor(Math.random() * 100) + 1,
      total: Math.floor(Math.random() * 100) + 1,
    });
  }
  return list;
}

export const Sale_List = goodSaleList(200);
